//
//  UIButton+EdgeInset.m
//  LearnObjectiveC
//
//  Created by 吴可高 on 17/4/3.
//  Copyright © 2017年 吴可高. All rights reserved.
//

#import "UIButton+EdgeInset.h"

@implementation UIButton (EdgeInset)

-(void)titleTextContentRect:(BadgeValuePosition)badgePosition;
{
    NSDictionary * params = @{@"0":@"titleTextContentLeftTop",
                              @"1":@"titleTextContentRightTop",
                              @"2":@"titleTextContentRightBottom",
                              @"3":@"titleTextContentBottom"};
    
    NSString * positionFormat = [NSString stringWithFormat:@"%zd",badgePosition];
    NSString * selector = params[positionFormat];
    if ([self respondsToSelector:NSSelectorFromString(selector)]) {
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        [self performSelector:NSSelectorFromString(selector)];
    #pragma clang diagnostic pop
    }
}

-(void)titleTextContentLeftTop
{
    self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    self.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
    
    CGFloat itemHeight = CGRectGetHeight(self.frame);
    CGFloat imgwith = CGRectGetWidth(self.imageView.frame);
    CGFloat itemwidth = CGRectGetWidth(self.frame);
    CGFloat titleHeight = CGRectGetHeight(self.titleLabel.frame);
    self.imageEdgeInsets = UIEdgeInsetsMake(titleHeight/3.f,imgwith/2.f, 0, 0);
    self.titleEdgeInsets = UIEdgeInsetsMake(-(itemHeight /2.f), -(itemwidth), 0, 0);
}

-(void)titleTextContentRightTop
{
    self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    self.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
    CGFloat imgwith = CGRectGetWidth(self.imageView.frame);
    CGFloat itemHeight = CGRectGetHeight(self.frame);
    CGFloat titleHeight = CGRectGetHeight(self.titleLabel.frame);
    self.imageEdgeInsets = UIEdgeInsetsMake(titleHeight/3.f,imgwith/2.f, 0, 0);
    self.titleEdgeInsets = UIEdgeInsetsMake(-(itemHeight /2.f),0.f, 0, 0);
}

-(void)titleTextContentRightBottom
{
    self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    self.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
    CGFloat imgwith = CGRectGetWidth(self.imageView.frame);
    CGFloat imgHeight = CGRectGetHeight(self.imageView.frame);
    CGFloat itemHeight = CGRectGetHeight(self.frame);
    self.imageEdgeInsets = UIEdgeInsetsMake(-imgHeight/3.f,imgwith/2.f, 0, 0);
    self.titleEdgeInsets = UIEdgeInsetsMake(itemHeight /2.f,0.f, 0, 0);
}

-(void)titleTextContentBottom;
{
    self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    CGFloat imgwith = CGRectGetWidth(self.imageView.frame);
    CGFloat imgHeight = CGRectGetHeight(self.imageView.frame);
    CGFloat itemwidth = CGRectGetWidth(self.frame);
    self.imageEdgeInsets = UIEdgeInsetsMake(-imgHeight/2.f, (itemwidth - imgwith)/2.f, 0, 0);
    self.titleEdgeInsets = UIEdgeInsetsMake(imgHeight, -(itemwidth-imgwith)/1.f, 0, 0);
}
@end
